
//
//  CalenderVC.m
//  CalenderDemo
//
//  Created by wr on 2018/7/20.
//  Copyright © 2018年 stzen. All rights reserved.
//

#import "CalenderVC.h"
#import "CalenderCell.h"
#import "CalenderManager.h"
#import "CalenderModel.h"
#import "CalenderHeaderView.h"
#import "UIView+Frame.h"

#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
#define IPHIONE6SCALE(X) ((X) * SCREENWIDTH / 375.0f)
#define WEEKVIEWHEIGHT 44

@interface CalenderVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong) UICollectionView * collectionView;
@property(nonatomic, strong) NSMutableArray *calenderDates;

@end

static NSString * const calenderCellID = @"calenderCellID";
static NSString * const calenderHeaderID = @"calenderHeaderID";

@implementation CalenderVC

#pragma mark - Life Cycle

- (void)dealloc {
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.calenderDates = [NSMutableArray array];
    
    [self initCalenderDataSource];
    [self loadUI];
}

#pragma mark - Calender dataSource

- (void)initCalenderDataSource {
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        CalenderManager *manager = [[CalenderManager alloc] init];
        NSArray *tempDataArray = [manager getCalendarDataSourceWithLimitedMonth:self.limitedMonth];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.calenderDates addObjectsFromArray:tempDataArray];
            [self.collectionView reloadData];
        });
    });
}

#pragma mark - UI

- (void)loadUI {
    NSUInteger width = IPHIONE6SCALE(54);
    NSUInteger height = IPHIONE6SCALE(60);
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(width, height);
    layout.headerReferenceSize = CGSizeMake(SCREENWIDTH, 50);// header
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + WEEKVIEWHEIGHT, width * 7, SCREENHEIGHT) collectionViewLayout:layout];
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[CalenderCell class] forCellWithReuseIdentifier:calenderCellID];
    [self.collectionView registerClass:[CalenderHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:calenderHeaderID];
    [self.view addSubview:self.collectionView];
    
    [self createWeekView];
}

- (void)createWeekView {
    UIView *weekView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), SCREENWIDTH, WEEKVIEWHEIGHT)];
    weekView.backgroundColor = [UIColor whiteColor];
    
    NSArray *weekArray = @[@"SUN",@"MON",@"TUE",@"WED",@"THU",@"FRI",@"SAT"];
    CGFloat width = IPHIONE6SCALE(54);
    CGFloat height = weekView.height;
    for (int i = 0; i < weekArray.count; i++) {
        UILabel *weekLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * width, 0, width, height)];
        weekLabel.backgroundColor = [UIColor whiteColor];
        weekLabel.font = [UIFont systemFontOfSize:16.f];
        weekLabel.text = weekArray[i];
        weekLabel.textAlignment = NSTextAlignmentCenter;
        [weekView addSubview:weekLabel];
        if (i == 0 || i == 6) { // weekend
            weekLabel.textColor = [UIColor lightGrayColor];
        } else {
            weekLabel.textColor = [UIColor redColor];
        }
    }
    
    [self.view addSubview:weekView];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.calenderDates.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    CalenderHeaderModel *headerItem = self.calenderDates[section];
    return headerItem.dateItems.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CalenderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:calenderCellID forIndexPath:indexPath];
    if (cell) {
        CalenderHeaderModel *headerItem = self.calenderDates[indexPath.section];
        CalenderModel *calenderItem = headerItem.dateItems[indexPath.row];
        cell.dateLabel.text = @"";
        cell.isCurrentDay = NO;
        
        if (calenderItem.day > 0) {
            cell.dateLabel.text = [NSString stringWithFormat:@"%ld", (long)calenderItem.day];
            
            if (calenderItem.day == [CalenderManager currentDateCompoent].day &&
                calenderItem.month == [CalenderManager currentDateCompoent].month &&
                calenderItem.year == [CalenderManager currentDateCompoent].year ) {
                cell.isCurrentDay = YES;
            }
        }
    }
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        CalenderHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:calenderHeaderID forIndexPath:indexPath];
        CalenderHeaderModel *headerItem = self.calenderDates[indexPath.section];
        headerView.headerLabel.text = headerItem.headerText;
        return headerView;
    }
    return nil;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end
